#!/usr/bin/env python
# -*- coding: UTF-8 -*-

try:
	import pygame
	import sys
	import lib.utils
	from pygame.locals import *
except ImportError, err:
	print "Could not load %s module." % (err)
	sys.exit(2)


def get_cursor_tile(type = 0):
	if type == 1:
		x = 32
	else:
		x = 0

	return lib.utils.get_cursor_icons().subsurface(((x, 32 * CURSOR), (32, 32)))

# "constants"
MELEE_ATK = 0
RANGED_ATK = 1
MOVE = 2
NA = 3
DEF_MAGIC = 4
CURSOR = 5
ATK_MAGIC = 6
BLANK = 7

'''
Emblem

This is the small animated icon that appears in the middle of the
cursor when the user is about to execute a game action (move, attack,
magic, etc.).
'''

class Emblem(pygame.sprite.DirtySprite):
	def __init__(self, layers, cursor):
		pygame.sprite.DirtySprite.__init__(self)
		self.frame = 0
		self.mov_seq = 0
		self.action = BLANK

		self.layers = layers
		self.cursor = cursor
		
		(x, y, w, h) = self.cursor.rect
		self.rect = pygame.rect.Rect(x + 16, y + 16, 32, 32)
		self.image = pygame.surface.Surface(self.rect.size).convert()
		self.image.set_colorkey(lib.utils.get_cursor_icons().get_at((0,0)), RLEACCEL)

		

	def set_action(self, action = None):
		if action is None:
			self.action = BLANK
			self.image.fill(self.image.get_colorkey())
			self.layers.remove(self)
			return

		if action == self.action:
			return

		self.action = action
		self.mov_seq = 0
		self.frame = 0
		self.image.blit(lib.utils.get_cursor_icons().subsurface(((0, 32 * self.action), (32, 32))), (0,0))
		self.layers.add(self, layer = 7)
		self.dirty = 1

	def update(self):
		(x, y, w, h) = self.cursor.rect
		self.rect = pygame.rect.Rect(x + 16, y + 16, 32, 32)

		self.frame = (self.frame + 1) % 20
		if self.action != BLANK and self.frame == 0:
			self.mov_seq  = (self.mov_seq + 1) % 2
			self.image.blit(lib.utils.get_cursor_icons().subsurface(((32 * self.mov_seq, 32 * self.action), (32, 32))), (0,0))
			self.dirty = 1
			return 1
		return 0
